package com.qzdsoft.erpcloud.controller.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.act.CenterCompany;
import com.qzdsoft.erpcloud.domain.basic.DataDictionaryType;
import com.qzdsoft.erpcloud.domain.basic.DataDictionaryValue;
import com.qzdsoft.erpcloud.service.act.CenterCompanyService;
import com.qzdsoft.erpcloud.service.basic.DataDictionaryTypeService;
import com.qzdsoft.erpcloud.service.basic.DataDictionaryValueService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.vo.query.basic.DataDictionaryValueInfo;
import com.qzdsoft.erpcloud.vo.query.basic.DataDictionaryValueQueryParam;
//import com.qzd.erpcloud.util.SessionUtil;
import com.qzdsoft.utils.SysUtils;
import com.qzdsoft.vo.EnumPo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.basic.BasicPreinstallEnum;

/**
 * Created by Admin
 */
@Controller
@RequestMapping("/basic/data/dictionary/value")
public class DataDictionaryValueController extends BaseController {

//    @Autowired
//    private RedisHelper redisHelper;

    @Autowired
    private DataDictionaryValueService basicDataDictionaryValueService;

    @Autowired
    private DataDictionaryTypeService basicDataDictionaryTypeService;

    @Autowired
    private CenterCompanyService actCenterCompanyService;

    @Permission(validate=false)
    @RequestMapping("/index")
    public Object index(HttpServletRequest request, Model model, @RequestParam("typeId") Integer typeId) {
        
        List<EnumPo> preinstallList = new ArrayList<EnumPo>();
        Map<Object, Object> preinstallMap = new HashMap<Object, Object>();
        for(BasicPreinstallEnum e : BasicPreinstallEnum.values()){
            SysUtils.enums(preinstallList, preinstallMap, e.getKey(), e.getVal());
        }

        model.addAttribute("preinstallList", preinstallList);
        model.addAttribute("preinstallMap", preinstallMap);

        model.addAttribute("basicDataDictionaryTypeList", basicDataDictionaryTypeService.selectAll());
        
        model.addAttribute("actCenterCompanyMap", actCenterCompanyService.getAllCompany());

        DataDictionaryType basicDataDictionaryType = basicDataDictionaryTypeService.selectById(typeId);

        CenterCompany actCenterCompany = null;
        if(null != basicDataDictionaryType.getCompany_id()){
            actCenterCompany = actCenterCompanyService.getById(basicDataDictionaryType.getCompany_id());
        }

        model.addAttribute("typeId", typeId);
        model.addAttribute("basicDataDictionaryType", basicDataDictionaryType);
        model.addAttribute("actCenterCompany", actCenterCompany);
        return "basic/data_dictionary/value_index";
    }
    /**
     * 跳转编辑或新增页
     * @param typeId
     * @param id
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/edit")
    public ModelAndView edit(Integer typeId,Integer id) {
        ModelAndView mv = new ModelAndView();
        List<EnumPo> preinstallList = new ArrayList<EnumPo>();
        Map<Object, Object> preinstallMap = new HashMap<Object, Object>();
        for(BasicPreinstallEnum e : BasicPreinstallEnum.values()){
            SysUtils.enums(preinstallList, preinstallMap, e.getKey(), e.getVal());
        }
        mv.addObject("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
        mv.addObject("preinstallList", preinstallList);
        mv.addObject("preinstallMap", preinstallMap);
        
        DataDictionaryType basicDataDictionaryType = basicDataDictionaryTypeService.selectById(typeId);

        CenterCompany actCenterCompany = null;
        if(null != basicDataDictionaryType.getCompany_id()){
            actCenterCompany = actCenterCompanyService.getById(basicDataDictionaryType.getCompany_id());
        }

        mv.addObject("typeId", typeId);
        mv.addObject("basicDataDictionaryType", basicDataDictionaryType);
        mv.addObject("actCenterCompany", actCenterCompany);
        mv.addObject("typeId", typeId);
        
        if(null != id){
//            return Response.getSuccess(basicDataDictionaryValueService.selectById(id));
            DataDictionaryValue dataDictionaryValue = basicDataDictionaryValueService.selectById(id);
            mv.addObject("dataDictionaryValue", dataDictionaryValue);
            mv.setViewName("/basic/data_dictionary/value_edit");
        }else {
            mv.setViewName("/basic/data_dictionary/value_add");
        }
        return mv;
    }
    /**
     * 编辑保存
     * @param request
     * @param basicDataDictionaryValue
     * @return
     */
    @ResponseBody
    @SysLog(description="编辑字典类型值保存")
    @RequestMapping("/edit_execute")
    public Object editExecute(HttpServletRequest request, DataDictionaryValue basicDataDictionaryValue) {
//        basicDataDictionaryValue.setCompany_id(SessionUtil.getLoggedCompanyId(request, basicDataDictionaryValue.getCompany_id()));
        return basicDataDictionaryValueService.saveOrUpd(basicDataDictionaryValue);
    }
    /**
     * 新增保存
     * @param request
     * @param basicDataDictionaryValue
     * @return
     */
    @ResponseBody
    @SysLog(description="新增字典类型值保存")
    @RequestMapping("/create_execute")
    public Object createExecute(HttpServletRequest request, DataDictionaryValue basicDataDictionaryValue) {
//        basicDataDictionaryValue.setCompany_id(SessionUtil.getLoggedCompanyId(request, basicDataDictionaryValue.getCompany_id()));
        return basicDataDictionaryValueService.saveOrUpd(basicDataDictionaryValue);
    }

    /**
     * 根据主键删除
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @SysLog(description="删除字典类型值")
    @RequestMapping("/del_execute")
    public Object delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
        return basicDataDictionaryValueService.del(id);
    }

    @ResponseBody
    @SysLog(description="查询字典类型值")
    @RequestMapping("/data")
    public LayTableResponse<DataDictionaryValueInfo> data(HttpServletRequest request, DataDictionaryValueQueryParam param) {
//        Integer companyId = SessionUtil.getLoggedCompanyId(request);
//        if(null != companyId){
//            param.setCompanyId(companyId);
//        }
        return basicDataDictionaryValueService.selectData(param.getTypeId(), param.getCompanyId(), param.getIsPreinstall(), param.getDecode(), param.getValue(),param.getTypeName(), param.getPage(), param.getLimit());
        
    }

}
